<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('工时列表')"/>
    <th:block th:include="include :: select2-css"/>
    <th:block th:include="include :: datetimepicker-css"/>
    <style>
        .day {
            color: #000000;
        }
    </style>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li th:if="${type=='summember'}">
                            <label>成员：</label>
                            <select name="user" class="form-control m-b">
                                <option value="">所有</option>
                                <option th:each="user : ${users}" th:text="${user.userName}"
                                        th:value="${user.userId}"></option>
                            </select>
                        </li>
                        <li th:if="${type=='sumproject'}">
                            <label>项目：</label>
                            <select name="projectId" class="form-control m-b">
                                <option value="">所有</option>
                                <option th:each="project : ${projects}" th:text="${project.name}"
                                        th:value="${project.id}" th:selected="${op == project.id}"></option>
                            </select>
                        </li>

                        <li>
                            <label>起始日期：</label>
                            <input type="text" class="input-sm form-control" id="datetimepicker-startTime"
                                   placeholder="请选择起始日期" name="startDate" th:value="${startDate}" autocomplete="off"
                                   required/>
                        </li>
                        <li>
                            <label>结束日期：</label>
                            <input type="text" class="input-sm form-control" id="datetimepicker-endTime"
                                   placeholder="请选择结束日期" name="endDate" th:value="${endDate}" autocomplete="off"
                                   required/>
                        </li>
                        <li>
                            <button type="button" class="btn btn-sm btn-warning" onclick="changeweek(-1)"><i
                                    class="fa fa-backward"></i> 上一周
                            </button>
                            <button type="button" class="btn btn-sm btn-danger" onclick="changeweek(0)"><i
                                    class="fa fa-eject"></i> 本周
                            </button>
                            <button type="button" class="btn btn-sm btn-warning" onclick="changeweek(1)"><i
                                    class="fa fa-forward"></i> 下一周
                            </button>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="searchTable()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset();resetTime();"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>


        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: select2-js"/>
<th:block th:include="include :: datetimepicker-js"/>
<script th:inline="javascript">
    var prefix = ctx + "system/cost";
    var tt = [[${type}]];

    function searchTable() {
        if ($.validate.form()) {
            $.table.search();
        }
    }

    $(function () {
        var leaderColumn = undefined;
        if (tt == "sumproject") {
            leaderColumn = {
                field: 'leader',
                title: '负责人'
            }
        }
        options = {
            url: prefix + "/" + [[${type}]],
            showFooter: true,
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            footerStyle: footerStyle,
            detailView: true,
            pagination: false,
            onExpandRow: function (index, row, $detail) {
                initChildTable(index, row, $detail);
            },
            columns: [{
                field: 'name',
                title: [[${type=='sumproject'?'项目':'成员'}]],
                footerFormatter: function (value) {
                    return [[${type=='sumproject'?'项目数：':'成员数：'}]] + value.length;
                }
            },
                {
                    field: 'sumList',
                    title: [[${type=='sumproject'?'成员':'项目'}]],
                    formatter: summaryFormat
                },
                {
                    field: 'costDay',
                    title: '人时',
                    formatter: numFormat,
                    footerFormatter: function (value) {
                        var sumCost = 0;
                        for (var i in value) {
                            sumCost += parseFloat(value[i].costDay);
                        }
                        return "总人时：" + numFormat(sumCost);
                    }
                }]
        };
        if(leaderColumn != null){
            options.columns.splice(1,0,leaderColumn);
        }
        $.table.init(options);
    });

    function footerStyle(column) {
        return {
            name: {
                css: {color: 'red', 'font-weight': 'normal'}
            },
            costDay: {
                css: {color: 'red', 'font-weight': 'normal'}
            }
        }[column.field]
    }

    initChildTable = function (index, row, $detail) {
        var childTable = $detail.html('<table style="table-layout:fixed"></table>').find('table');
        $(childTable).bootstrapTable({
            data: row.list,
            columns: [{
                field: [[${type=='sumproject'?'userName':'projectName'}]],
                title: [[${type=='sumproject'?'成员':'项目'}]]
            },
                {
                    field: 'remark',
                    title: '内容',
                },
                {
                    title: '时间',
                    formatter: infoSummaryFormat
                },
                {
                    field: 'costDay',
                    title: '人时',
                    formatter: numFormat,
                }]
        });
    };

    function numFormat(value) {
        return value.toFixed(1) * 1;
    }

    function summaryFormat(value) {
        let str = " ";
        for (let i = 0; i < value.length; i++) {
            let v = value[i];
            str += v.sumName + "（" + v.sumDay + "） ";
        }
        return str;
    }

    function infoSummaryFormat(value, row, index) {
        return row.startDate + '~' + row.endDate;
    }

    $("#datetimepicker-startTime").datetimepicker({
        format: 'yyyy-mm-dd',
        minView: "month",
        todayBtn: true,
        autoclose: true,
        weekStart: 1,
        endDate: new Date(),
        daysOfWeekDisabled: [0, 1, 2, 3, 4, 5]
    }).on('changeDate', function (event) {
        event.preventDefault();
        event.stopPropagation();
        resetTime();
    });

    $("#datetimepicker-endTime").datetimepicker({
        format: 'yyyy-mm-dd',
        minView: "month",
        todayBtn: true,
        yesterdayBtn: true,
        autoclose: true,
        weekStart: 1,
        startDate: [[${startDate}]],
        endDate: new Date(),
        daysOfWeekDisabled: [0, 1, 2, 3, 4, 6]
    }).on('changeDate', function (event) {
        event.preventDefault();
        event.stopPropagation();
        resetTime();
    });

    function resetTime() {
        $('#datetimepicker-endTime').datetimepicker('setStartDate', $("#datetimepicker-startTime").val());
        $("#datetimepicker-startTime").datetimepicker('setEndDate', $("#datetimepicker-endTime").val());
    }

    function changeweek(week) {
        $.ajax({
            url: prefix + "/changeweek?week=" + week + "&date=" + $("#datetimepicker-endTime").val(),
            type: "POST",
            success: function (result) {
                $("#datetimepicker-endTime").val(result[1]);
                $("#datetimepicker-startTime").val(result[0]);
                resetTime();
            }
        });
    }

</script>
</body>
</html>